:: NDK 交叉编译thrift c++ 库
:: NDK version 19 or above required
:: cmake 3.11.1 or ablove required
:: Flags you can use to change this behavior:
::
::   /DEBUG            - debug instead of release
::   /NOBUILD          - skip cmake build - useful if you just
::                       want to generate a solution
:: 编译 arm5(armeabi) 平台版本要求　NDK 16 及以下版本.
:: 编译 其他平台版本要求　NDK 19 及以上版本.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET sh_folder=%~dp0
SET PACKAGE_NAME=thrift

:: 检测是否安装NDK,没有安装NDK则报错退出
IF NOT DEFINED ANDROID_NDK (
    echo "ERROR:environment variable ANDROID_NDK not defined" 
    EXIT /B 255
    )

IF NOT EXIST "%ANDROID_NDK%" (
    echo "ERROR:invalid environment variable ANDROID_NDK=%ANDROID_NDK%" 
    EXIT /B 255
    )

SET BUILDTYPE=Release
SET OPT_BUILD=1
SET OPT_COMPILER=OFF

:: parse command arguments
:loop
IF x%1 == x goto :pare_end
IF /I "%1" == "/DEBUG"        SET BUILDTYPE=Debug
IF /I "%1" == "/NOBUILD"      SET OPT_BUILD=0
SHIFT
goto :loop
:pare_end

:: 目标平台
IF NOT DEFINED ANDROID_ABI SET ANDROID_ABI=armeabi-v7a

IF "%ANDROID_ABI%"=="armeabi" ( 
    SET ARCH=ARMEABI
) ELSE IF "%ANDROID_ABI%"=="armeabi-v7a" ( 
    SET ARCH=ARMEABI-V7A
) ELSE IF "%ANDROID_ABI%"=="arm64-v8a" (
    SET ARCH=ARM64-V8A
) ELSE IF "%ANDROID_ABI%"=="x86" (
    SET ARCH=X86
) ELSE IF "%ANDROID_ABI%"=="x86_64" (
    SET ARCH=x64
) ELSE (
    ECHO "Invalid Android ABI: %ANDROID_ABI%." 
    EXIT /B 255
)

:: boost 安装位置
IF NOT DEFINED BOOST_ROOT SET BOOST_ROOT="%sh_folder%dist\boost_1_69_0_android\%ANDROID_ABI%"
:: thrift 源码位置
IF NOT DEFINED THRIFT_FOLDER SET THRIFT_FOLDER="%sh_folder%thrift"
:: 编译输出路径
SET BUILDDIR=%sh_folder%build\%PACKAGE_NAME%_android\%ANDROID_ABI%
:: 安装路径
SET OUTDIR=%sh_folder%dist\%PACKAGE_NAME%_android\%ANDROID_ABI%

IF NOT EXIST "%BOOST_ROOT%" (
    ECHO NOT FOUND BOOST FOLDER %BOOST_ROOT%
    EXIT /B 255
)

ECHO =========================================================================
ECHO     Configuration: %PACKAGE_NAME% %ANDROID_ABI%:%BUILDTYPE%
ECHO   Build Directory: %BUILDDIR%
ECHO Install Directory: %OUTDIR%
ECHO   Boost Directory: %BOOST_ROOT%
ECHO =========================================================================

IF EXIST "%BUILDDIR%" ( RMDIR "%BUILDDIR%" /s/q || EXIT /B )
MKDIR "%BUILDDIR%" || EXIT /B
CD "%BUILDDIR%" || EXIT /B

:: NOTE:
:: -DBoost_ADDITIONAL_VERSIONS="1.69.0;1.69" 
:: 因为 cmake 3.11.1中的 FindBoost.cmake 不能识别boost 1.69 版本,所以这里要用定义 Boost_ADDITIONAL_VERSIONS 让 FindBoost.cmake 能正确识别 1.69 版本.
:: cmake 3.17.1 以后的版本不需要指定该参数.
:: -DBoost_COMPILER=-clang
:: 因为 cmake 3.11.1 中的 FindBoost.cmake 还不支持 clang 编译器自动识别,所以这里通过定义 Boost_COMPILER 指定搜索库(library)时的库名后缀.
:: 使用 camek 3.17.1 以后的版本可以不指定此参数
:: -DCMAKE_CXX_COMPILER_ARCHITECTURE_ID=%ARCH%
:: boost 1.69 版本以后生成的库名中包含CPU体系的识别码,比如 libboost_atomic-clang-mt-a32-1_69.a,这里的a32代表arm32.
:: 在FindBoost.cmake中这个值由 CMAKE_CXX_COMPILER_ARCHITECTURE_ID 计算得出,如果不指定 CMAKE_CXX_COMPILER_ARCHITECTURE_ID,FindBoost.cmake 就无法正确计算出搜索库的文件名.
:: cmake 3.17.1版本以后的 FindBoost.cmake 增加了 Boost_ARCHITECTURE 参数,可以直接指定这个识别码,比如 -x32,代码X86,-a32代表arm32
:: -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH 
:: -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH 
:: 工具链文件 android.toolchain.cmake 中上面的参数都定义为ONLY,这里指为BOTH,否则 FindBoost.cmake 不能正确找到 include 文件夹和library 

CMAKE.EXE %THRIFT_FOLDER% ^
    -G "Unix Makefiles" ^
    -DCMAKE_INSTALL_PREFIX=%OUTDIR% ^
    -DCMAKE_BUILD_TYPE=%BUILDTYPE% ^
    -DBOOST_ROOT=%BOOST_ROOT% ^
    -DBoost_ADDITIONAL_VERSIONS="1.69.0;1.69" ^
    -DBoost_NO_SYSTEM_PATHS=ON ^
    -DBoost_COMPILER=-clang ^
    -DWITH_BOOSTTHREADS=ON ^
    -DWITH_C_GLIB=OFF ^
    -DWITH_LIBEVENT=OFF ^
    -DWITH_OPENSSL=OFF ^
    -DWITH_SHARED_LIB=OFF ^
    -DWITH_STATIC_LIB=ON ^
    -DWITH_JAVA=OFF ^
    -DWITH_PYTHON=OFF ^
    -DWITH_HASKELL=OFF ^
    -DWITH_LIBEVENT=OFF ^
    -DWITH_ZLIB=ON ^
    -DWITH_QT4=OFF ^
    -DBUILD_COMPILER=OFF ^
    -DBUILD_TESTING=OFF ^
    -DBUILD_EXAMPLES=OFF ^
    -DBUILD_TUTORIALS=OFF ^
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON ^
    -DCMAKE_CXX_COMPILER_ARCHITECTURE_ID=%ARCH% ^
    -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH ^
    -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH ^
    -DANDROID_ABI=%ANDROID_ABI% ^
    -DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%\build\cmake\android.toolchain.cmake ^
     || EXIT /B


IF %OPT_BUILD% == 1 (
  CD %BUILDDIR%
  CMAKE.EXE --build . --target install -- -j8 || EXIT /B
)

ENDLOCAL